﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public class SystemOptions
{
    public OptionValue this[string Key]
    {
        get
        {
            string SystemOptionKey = Shove._Web.WebConfig.GetAppSettingsString("SystemPreFix") + "SystemOptions";
            DataTable dt = null;

            try
            {
                dt = (DataTable)System.Web.HttpContext.Current.Application[SystemOptionKey];
            }
            catch { }

            if (dt == null)
            {
                dt = new DAL.Tables.tbl_Options().Open("", "", "");

                if (dt == null)
                {
                    throw new Exception("tbl_Options 表读取发生错误，请检查数据连接或者数据库是否完整");
                }

                try
                {
                    System.Web.HttpContext.Current.Application.Lock();
                    System.Web.HttpContext.Current.Application.Add(SystemOptionKey, dt);
                }
                catch { }
                finally
                {
                    try
                    {
                        System.Web.HttpContext.Current.Application.UnLock();
                    }
                    catch { }
                }
            }

            if (dt == null)
            {
                throw new Exception("Tbl_Options 表读取发生错误，请检查数据连接或者数据库是否完整");
            }

            DataRow[] drs = dt.Select("[Key]='" + Key + "'");

            try
            {
                if ((drs == null) || (drs.Length < 1))
                {
                    throw new Exception("Tbl_Options 表读取发生错误，请检查数据连接或者是否该表拥有 Key 值为 " + Key + " 记录");
                }
            }
            catch (SystemException se)
            {
                throw new Exception(se.Message);
            }

            string Result = drs[0]["Value"].ToString();

            if (Result == "Bin")
            {
                try
                {
                    Result = Shove._String.Decompress((byte[])drs[0]["ValueBinary"]);
                }
                catch
                {
                    Result = "";
                }
            }

            return new OptionValue(Result);
        }

        set
        {
            string SystemOptionKey = Shove._Web.WebConfig.GetAppSettingsString("SystemPreFix") + "SystemOptions";

            try
            {
                System.Web.HttpContext.Current.Application.Lock();
                System.Web.HttpContext.Current.Application.Remove(SystemOptionKey);
            }
            catch { }
            finally
            {
                try
                {
                    System.Web.HttpContext.Current.Application.UnLock();
                }
                catch { }
            }

            string ReturnDescription = "";

            if (DAL.Functions.F_GetOption(Key) == "Bin")
            {
                DAL.Procedures.P_SetOptionBinary(Key, Shove._String.Compress(value.Value.ToString()), ref ReturnDescription);
            }
            else
            {
                DAL.Procedures.P_SetOptions(Key, value.Value.ToString());
            }
        }
    }
}